<!DOCTYPE html>
<html lang="zh-cn">
<head>
    <meta charset="UTF-8">
<meta name="viewport"
      content="width=device-width, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">

    <meta name="author" content="孟建都">


    <meta name="subtitle" content="孟建都的博客">


    <meta name="description" content="孟建都的博客">


    <meta name="keywords" content="孟建都,博客">


<title>Vue中使用Async和Await代替Promise的then方法 | 孟建都的博客</title>



    <link rel="icon" href="/blog/favicon.ico">




    <!-- stylesheets list from _config.yml -->
    
    <link rel="stylesheet" href="/blog/css/style.css">
    



    <!-- scripts list from _config.yml -->
    
    <script src="/blog/js/script.js"></script>
    
    <script src="/blog/js/tocbot.min.js"></script>
    



    
    
        
    


<meta name="generator" content="Hexo 5.1.1"></head>
<body>
    <div class="wrapper">
        <header>
    <nav class="navbar">
        <div class="container">
            <div class="navbar-header header-logo"><a href="/blog/">Jarrett&#39;s Blog</a></div>
            <div class="menu navbar-right">
                
                    <a class="menu-item" href="/blog/archives">文章</a>
                
                    <a class="menu-item" href="/blog/about">关于</a>
                
                <input id="switch_default" type="checkbox" class="switch_default">
                <label for="switch_default" class="toggleBtn"></label>
            </div>
        </div>
    </nav>

    
    <nav class="navbar-mobile" id="nav-mobile">
        <div class="container">
            <div class="navbar-header">
                <div>
                    <a href="/blog/">Jarrett&#39;s Blog</a><a id="mobile-toggle-theme">·&nbsp;Light</a>
                </div>
                <div class="menu-toggle" onclick="mobileBtn()">&#9776; Menu</div>
            </div>
            <div class="menu" id="mobile-menu">
                
                    <a class="menu-item" href="/blog/archives">文章</a>
                
                    <a class="menu-item" href="/blog/about">关于</a>
                
            </div>
        </div>
    </nav>

</header>
<script>
    var mobileBtn = function f() {
        var toggleMenu = document.getElementsByClassName("menu-toggle")[0];
        var mobileMenu = document.getElementById("mobile-menu");
        if(toggleMenu.classList.contains("active")){
           toggleMenu.classList.remove("active")
            mobileMenu.classList.remove("active")
        }else{
            toggleMenu.classList.add("active")
            mobileMenu.classList.add("active")
        }
    }
</script>
        <div class="main">
            <div class="container">
    
    

    
    <article class="post-wrap">
        <header class="post-header">
            <h1 class="post-title">Vue中使用Async和Await代替Promise的then方法</h1>
            
                <div class="post-meta">
                    
                        Author: <a itemprop="author" rel="author" href="/">孟建都</a>
                    

                    
                        <span class="post-time">
                        Date: <a href="#">九月 7, 2020&nbsp;&nbsp;10:49:07</a>
                        </span>
                    
                    
                </div>
            
        </header>

        <div class="post-content">
            <ul>
<li><p>写一个promise方法</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">adList</span>(<span class="params"></span>) </span>&#123;</span><br><span class="line">  <span class="keyword">return</span> <span class="keyword">new</span> <span class="built_in">Promise</span>(<span class="function">(<span class="params">resolve,reject</span>)=&gt;</span>&#123;</span><br><span class="line">    fetch.fetch(&#123;</span><br><span class="line">      url: <span class="string">&#x27;https://www.test.com&#x27;</span>,</span><br><span class="line">      responseType: <span class="string">&#x27;json&#x27;</span>,</span><br><span class="line">      method: <span class="string">&#x27;get&#x27;</span>,</span><br><span class="line">      success: <span class="function"><span class="keyword">function</span>(<span class="params">response</span>) </span>&#123;  </span><br><span class="line">        resolve(response.data)</span><br><span class="line">      &#125;,</span><br><span class="line">      fail: <span class="function"><span class="keyword">function</span>(<span class="params">data, code</span>) </span>&#123; </span><br><span class="line">        reject(data,code)</span><br><span class="line">      &#125;</span><br><span class="line">    &#125;)</span><br><span class="line">  &#125;)</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
</li>
<li><p>以前使用promise的then方法写法</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line">getAdListData() &#123;</span><br><span class="line">  adList().then(<span class="function"><span class="params">res</span> =&gt;</span> &#123;</span><br><span class="line">    <span class="keyword">if</span> (res.code === <span class="number">1</span> ) &#123;</span><br><span class="line">      <span class="built_in">this</span>.bannerData = res.data</span><br><span class="line">    &#125;</span><br><span class="line">  &#125;)</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></li>
<li><p>改写为async\await写法</p>
</li>
</ul>
<p>在定义function或者方法的时候在前面加上async并且在想要同步执行的地方使用 await 加上方法名称 并且能够接收到promise的返回值</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">async</span> getAdListData() &#123;</span><br><span class="line">  <span class="keyword">const</span> res = <span class="keyword">await</span> adList()</span><br><span class="line">  <span class="keyword">if</span> (res.code === <span class="number">1</span>) &#123;</span><br><span class="line">    <span class="built_in">this</span>.bannerData = res.data</span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
        </div>

        
            <section class="post-copyright">
                
                    <p class="copyright-item">
                        <span>Author:</span>
                        <span>孟建都</span>
                    </p>
                
                
                
                

            </section>
        
        <section class="post-tags">
            <div>
                <span>Tag(s):</span>
                <span class="tag">
                    
                </span>
            </div>
            <div>
                <a href="javascript:window.history.back();">back</a>
                <span>· </span>
                <a href="/blog/">home</a>
            </div>
        </section>
        <section class="post-nav">
            
                <a class="prev" rel="prev" href="/blog/2020/09/07/%E5%88%9D%E8%AF%86%E5%BE%AE%E6%9C%8D%E5%8A%A1/">初识微服务</a>
            
            
            <a class="next" rel="next" href="/blog/2020/09/07/vue%E8%B7%B3%E8%BD%AC%E4%B9%8B%E5%90%8E%E4%BF%9D%E5%AD%98%E6%9F%A5%E8%AF%A2%E7%9A%84%E6%95%B0%E6%8D%AE%EF%BC%88%E6%B5%8F%E8%A7%88%E5%99%A8%E8%BF%94%E5%9B%9E%E8%83%BD%E5%A4%9F%E4%BF%9D%E5%AD%98%E6%9F%A5%E8%AF%A2%E7%9A%84%E6%9D%A1%E4%BB%B6%EF%BC%89/">vue跳转之后保存查询的数据（浏览器返回能够保存查询的条件）</a>
            
        </section>


    </article>
</div>

        </div>
        <footer id="footer" class="footer">
    <div class="copyright">
        <span>© 孟建都 | Powered by <a href="https://hexo.io" target="_blank">Hexo</a> & <a href="https://github.com/Siricee/hexo-theme-Chic" target="_blank">Chic</a></span>
    </div>
</footer>

    </div>
</body>
</html>
